# -*- coding: utf-8 -*-            
# @Author : HJH
# @Time : 2024/8/21 15:53
# @Use :
import json

from loguru import logger

from api.tongc_policy_api import award_route_query, award_policy_submit
import concurrent.futures


def log_config():
    # 添加输出处理程序，将日志输出到文件
    logger.add("../logs/info_file.log", level="INFO")
    logger.add("../logs/error_file.log", level="ERROR")


def task_function(item, p_id):
    award_policy_submit(item["startPort"], item["endPort"], item["flightDate"], item["flightNo"], p_id)
    pass


if __name__ == '__main__':
    log_config()

    guid_mapping = {
        # "9H": "e09ec0bd9bf04e6d8c7608259994b25c",
        # "SC": "dfd646e06057455e9e98c2323cec3f80",
        # "CZ": "8c19f441db8146e3b06beaea582c3dd3",
        # "MF": "b9b36fe29e864a21a1717de648af98a6",
        # "HU": "a378f97fb74e414a99e52d074dfa624a",
        # "GJ": "d5dafd5c7ff14925961e529b36b53e91",
        # "HO": "475242ba76df4a848ccf312bcd93fb01",
        # "GS": "ecbf4efef4044aeba8c8fcfe76746a12",
        # "9C": "b196ca064bb947fe91d3ff20e6d2c348",
        # "KN": "3114e2c9fc08417ab5fa76760f8b2ad4",
        # "JD": "3114e2c9fc08417ab5fa76760f8b2ad4",
        # "KY": "feb8b4d2098043148be3178142239a83",
        # "AQ": "32048075c3894e24bbe2b8991ee44bc7",
        "RY": "2bdd446f347b474385e29afea6f20cef",
        # "SC": "c65911302f7548328d4858c927340db7",
        # "Y8": "15e4abb582944d2d82c4462616b62ba4",
    }
    guid_mapping_keys = list(guid_mapping.keys())

    with open('../docs/RY-航班数据.json') as f:
        # 加载 JSON 数据并转换为字典
        res_data_list = json.load(f)

    # res_data_list = award_route_query()
    # res_data_list = res_data_list['data']
    res_data_list = [rd for rd in res_data_list if rd['flightDate'] >= "2024-08-27" and rd['airlineCode'] in guid_mapping_keys]

    with concurrent.futures.ThreadPoolExecutor(max_workers=10) as executor:
        # 使用列表 comprehension 存储任务的 Future 对象
        # futures = [executor.submit(task_function, rd, guid_mapping.get(rd.get("airlineCode", ""))) for rd in res_data_list['data']]

        for rd in res_data_list:
            guid = guid_mapping.get(rd.get("airlineCode"))
            if guid is None:
                continue
            executor.submit(task_function, rd, guid)
